package com.lcq.study.leetcode;

/**
 * @ClassName T20220331
 * @Description
 * @Author lichuanqi
 * @Date 2022/3/31 9:14
 * @Version 1.0
 **/
public class T20220331 {
    public static void main(String[] args) {
        T20220331 solution = new T20220331();

        int [] a = {1,2,3,4,5};
        int maxProfit = solution.maxProfit(a);
        System.out.println(maxProfit);
    }
    /**
     * @Author lichuanqi
     * @Description 知道第二天股票涨就就买，股票跌就卖
     * @Date 10:19 2022/4/3
     * @Param [prices]
     * @return int
     **/
    public int maxProfit(int[] prices) {
        int profit = 0;
        if (prices.length < 2){
            return 0;
        }
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i-1]){
                //计算价格之差表示纯利润，不记录股票买入价格
                profit += prices[i] - prices[i-1];
            }
        }
        return profit;
    }


    public int maxProfit1(int[] prices) {
        if (prices.length < 2){
            return 0;
        }
        int [][] table = new int[prices.length][prices.length];
        //买入卖出构建table
        for (int i = 0; i < prices.length; i++) {
            for (int j = i; j < prices.length; j++) {
                table[i][j] = prices[j] - prices[i];
                System.out.print(table[i][j]);
                System.out.print(" ");
            }
            System.out.println("");
        }
        StringBuilder stringBuilder = new StringBuilder();
        int profit = 0;
        int maxProfit = 0;
        int  start = 0;
        while(start<prices.length){
            profit = 0;
            for (int i = start; i < prices.length; i++) {
                for (int j = i; j < prices.length; j++) {
                    if (table[i][j] > 0){
                        stringBuilder.append("("+i+","+j+"),");
                        profit += table[i][j];
                        i = j;
                        break;
                    }
                }
            }
            if (profit > maxProfit){
                System.out.println(stringBuilder);
                maxProfit = profit;
            }
            start++;
        }

        stringBuilder.delete(0, stringBuilder.capacity()-1);
        return maxProfit;
    }
}
